package de.westranger.forex.trading.preprocessing;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.zip.GZIPOutputStream;

public class PrepareTickData {

	public static String fname = "05_EUR_USD_Week1";
	public static String pathIn = "E:\\EUR_USD_2013\\raw\\"+fname+".csv";
	public static String pathOut = "E:\\EUR_USD_2013\\pre\\"+fname+"_pre.csv.gz";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final String[] files = new String[] { 
				"01_EUR_USD_Week2", "01_EUR_USD_Week3", "01_EUR_USD_Week4",
				"01_EUR_USD_Week5", "02_EUR_USD_Week1", "02_EUR_USD_Week2",
				"02_EUR_USD_Week3", "02_EUR_USD_Week4", "03_EUR_USD_Week1",
				"03_EUR_USD_Week2"};
		
		for(String s:files){
			fname = s;
			pathIn = "E:\\EUR_USD_2013\\raw\\"+fname+".csv";
			pathOut = "E:\\EUR_USD_2013\\pre\\"+fname+"_pre.csv.gz";
			convert();
		}
		
	}
	
	private static final void convert(){
		try {
			final BufferedReader br = new BufferedReader(new FileReader(new File(pathIn)));
			final BufferedOutputStream bw = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(new File(pathOut))));

			String line = null;
			String str = null;
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.S");
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			// skip first line
			if(br.ready()){
				br.readLine();
			}
			
			bw.write("TIME;LOW;HIGH\n".getBytes());
//			bw.append("TIME;LOW;HIGH\n");
			while(br.ready()){
				line = br.readLine();
				StringTokenizer stk = new StringTokenizer(line,",");
				
				for(int i=0;i<4;i++){
					str = stk.nextToken();
				}
				str = str.replace('-', '/');
				if(str.length() == 29){
					str = str.substring(0, str.length()-6);					
				}
				
				Date d = null;
				
				try {
					d = sdf1.parse(str);
				} catch (ParseException e) {
					try {
						d = sdf2.parse(str);
					} catch (ParseException e1) {
						e1.printStackTrace();
					}
				}
				
				//System.out.println(str + " --- " + sdf1.format(d));

				bw.write(Long.toString(d.getTime()).getBytes());
				bw.write(';');
				bw.write(stk.nextToken().getBytes());
				bw.write(';');
				bw.write(stk.nextToken().getBytes());
				bw.write('\n');
				
//				bw.append(Long.toString(d.getTime()));
//				bw.append(';');
//				bw.append(stk.nextToken());
//				bw.append(';');
//				bw.append(stk.nextToken());
//				bw.append('\n');
			}
			
			br.close();
			bw.close();
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
